﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL.Access;
using System.Data;
using System.IO;
using System.Text;
using Web.Class;

namespace Web.ashx.YSSF
{
    /// <summary>
    /// YinHangDLDC 的摘要说明
    /// </summary>
    public class YinHangDLDC : IHttpHandler
    {
        SpHelper _spHelper = new SpHelper();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string rsNo = "-1";
            string rsMsg = "";

            var i_id = context.Request["i_id"].ToString();
            var wenjianm = context.Request["wenjianm"].ToString();
            DataTable dt = this._spHelper.P_YS_YINHANGPLDLWJJK(i_id.ToString(), 2, out rsNo, out rsMsg).Tables[0];
            var resp = context.Response;
            resp.ContentType = "text/plain;charset=gb2312";
            //resp.Charset = "UTF-8";
            resp.ContentEncoding = Encoding.GetEncoding("gb2312"); //注意编码
            resp.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(wenjianm, Encoding.GetEncoding("gb2312")));
            var str = new StringBuilder();
            var sw = new StringWriter();
            
            //计算空格长度
            int len1 = 0,len2=0,len3=0,len4=0,len5=0,len6=0,len7=0,len8=0;
            int lenT = 0;
            for (int i=1;i<dt.Rows.Count;i++)
            {
                lenT = Encoding.GetEncoding("gb2312").GetBytes(dt.Rows[i]["S_RIQI"].ToString().Trim() + dt.Rows[i]["S_YONGHUM"].ToString().Trim()).Length;
                if (lenT > len1)
                {
                    len1 = lenT;
                }
                lenT = Encoding.GetEncoding("gb2312").GetBytes(dt.Rows[i]["S_KAIHUZH"].ToString().Trim()).Length;
                if (lenT > len2)
                {
                    len2 = lenT;
                }
                lenT = Encoding.GetEncoding("gb2312").GetBytes(dt.Rows[i]["S_KAIHUYH"].ToString().Trim()).Length;
                if (lenT > len3)
                {
                    len3 = lenT;
                }
                lenT = Encoding.GetEncoding("gb2312").GetBytes(dt.Rows[i]["N_JINE"].ToString().Trim()).Length;
                if (lenT > len4)
                {
                    len4 = lenT;
                }
                lenT = Encoding.GetEncoding("gb2312").GetBytes(dt.Rows[i]["S_HETONGH"].ToString().Trim() + dt.Rows[i]["S_LEIXING"].ToString().Trim() + dt.Rows[i]["I_YM"].ToString().Trim() + dt.Rows[i]["S_JIHUASL"].ToString().Trim()).Length;
                if (lenT > len5)
                {
                    len5 = lenT;
                }
            }
            /*
            len1 = len1 + 10;
            len2 = len2 + 5;
            len3 = len3 + 5;
            len4 = len4 + 2;
            len5 = len5 + 10;
            len6 = len7= 12;
            */
            len1 = 72;
            len2 = 30;
            len3 = 40;
            len4 = 15;
            len5 = 66;
            len6 = 11;
            len7 = 7;
            len8 = 64;
            //取出第一行值做空格处理
            DataRow topRow = dt.Rows[0];
            lenT = Encoding.GetEncoding("gb2312").GetBytes((topRow[0].ToString() + topRow[1].ToString() + topRow[2].ToString())).Length;
            len1 = len1 < lenT ? lenT + 2 : len1;
            //str.Append(topRow[0])
            //    .Append(new string(' ', (len1 - lenT) / 2)).Append(topRow[1])
            //    .Append(new string(' ', (len1 - lenT) % 2 == 0 ? (len1 - lenT) / 2 : ((len1 - lenT) / 2 + 1))).Append(topRow[2])
            //;
            str.Append(topRow[0] + "      " + topRow[1] + "   " + topRow[2]);
            str.Append(new string(' ', len2 + len3)).Append(topRow[3]);            
            //lenT = len1 + len2 + len3 + len4 - Encoding.GetEncoding("gb2312").GetBytes(str.ToString()).Length + Encoding.GetEncoding("gb2312").GetBytes(dt.Rows[1]["S_HETONGH"].ToString().Trim()).Length;
            lenT = len1 + len2 + len3 + len4 - Encoding.GetEncoding("gb2312").GetBytes(str.ToString()).Length + 47;
            str.Append(new string(' ', lenT)).Append(topRow[4]);
            lenT = len1 + len2 + len3 + len4 + len5 - Encoding.GetEncoding("gb2312").GetBytes(str.ToString()).Length;
            str.Append(new string(' ', lenT)).Append(topRow[5])
               .Append(new string(' ', len6 + len7 + len8 - Encoding.GetEncoding("gb2312").GetBytes(topRow[5].ToString()).Length));

            sw.WriteLine(str.ToString());
            dt.Rows.RemoveAt(0);
            string st = null;
            //逐行处理数据   
            foreach (DataRow row in dt.Rows)
            {
                str.Clear();
                str.Append(row["S_RIQI"].ToString().Trim()+row["S_YONGHUM"].ToString().Trim() + new string(' ', len1 - Encoding.GetEncoding("gb2312").GetBytes(row["S_RIQI"].ToString().Trim()+row["S_YONGHUM"].ToString().Trim()).Length))
                    .Append(row["S_KAIHUZH"].ToString().Trim() + new string(' ', len2 - Encoding.GetEncoding("gb2312").GetBytes(row["S_KAIHUZH"].ToString().Trim()).Length))
                    .Append(row["S_KAIHUYH"].ToString().Trim() + new string(' ', len3 - Encoding.GetEncoding("gb2312").GetBytes(row["S_KAIHUYH"].ToString().Trim()).Length))
                    .Append(row["N_JINE"].ToString().Trim() + new string(' ', len4 - Encoding.GetEncoding("gb2312").GetBytes(row["N_JINE"].ToString().Trim()).Length))
                ;
                st = row["S_HETONGH"].ToString().Trim() + row["S_YONGHUH"].ToString().Trim() + row["S_LEIXING"].ToString().Trim() + row["I_YM"].ToString().Trim() + row["S_JIHUASL"].ToString().Trim();
                str.Append(st + new string(' ', len5 - Encoding.GetEncoding("gb2312").GetBytes(st).Length));
                st =row["S_SHIYONGSL"].ToString().Trim();
                str.Append(st + new string(' ', len6 - Encoding.GetEncoding("gb2312").GetBytes(st).Length));
                st =row["S_JIAJIASL"].ToString().Trim();
                str.Append(st + new string(' ', len7 - Encoding.GetEncoding("gb2312").GetBytes(st).Length));
                //str.Append(row["S_KAIHUHM"]);
                st = row["S_KAIHUHM"].ToString().Trim();
                str.Append(st + new string(' ', len8 - Encoding.GetEncoding("gb2312").GetBytes(st).Length));
                sw.WriteLine(str.ToString());
            }
            sw.Close();
            resp.Write(sw);
            resp.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}